package org.xxpay.manage.common.ctrl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.common.util.XXPayUtil;
import org.xxpay.manage.common.config.MainConfig;

import java.util.UUID;

@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/upload")
public class FileUploadController extends BaseController {

    private static final MyLog logger = MyLog.getLog(FileUploadController.class);

    @Autowired
    private MainConfig mainConfig;

    /** 上传商户进件相关资料 */
    @RequestMapping("/putImg")
    public XxPayResponse putImg(@RequestParam("file") MultipartFile file) {

        if( file == null ) return XxPayResponse.build(RetEnum.RET_COMM_PARAM_NOT_FOUND);

        try {
            String fileUrl = commonSaveUpdFile(file, null, null); //统一使用uuid格式
            return XxPayResponse.buildSuccess(fileUrl);

        } catch (Exception e) {
            logger.error("upload error, fileName = {}", file == null ? null :file.getOriginalFilename(), e);
            return XxPayResponse.build(RetEnum.RET_COMM_OPERATION_FAIL);
        }
    }

}